package com.zwh.se.fileEncrypt;

import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.CompressionLevel;
import net.lingala.zip4j.model.enums.EncryptionMethod;
import org.apache.commons.io.IOUtils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


public class ZipFileEncrypt {

    public static void main(String[] args) throws Exception {
//        try {
//            ZipCompressor.compressFile("D:\\idea\\file\\test.csv", "D:\\idea\\file\\output.zip");
////            ZipCompressor.lockZipFile("D:\\idea\\file\\output.zip");
//            System.out.println("文件压缩和加锁成功！");
//        } catch (IOException  e) {
//            e.printStackTrace();
//        }
        test2();
    }

//    public static void test(){
//        ZipArchiveOutputStream zos = new ZipArchiveOutputStream(new FileOutputStream(zipFile));
//        // 设置加密方式
//        zos.setEncoding("GBK");
//        zos.setMethod(ZipArchiveOutputStream.DEFLATED);
//        zos.setEncryptName(true);
//        zos.setPassword("password");
//        // 将文件写入ZipArchiveOutputStream
//        FileInputStream fis = new FileInputStream(file);
//        ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file.getName());
//        zos.putArchiveEntry(zipArchiveEntry);
//        IOUtils.copy(fis, zos);
//        zos.closeArchiveEntry();
//        // 关闭ZipArchiveOutputStream
//        zos.close();
//    }
    public static void test2() throws Exception {
        ZipParameters zipParameters = new ZipParameters();
        zipParameters.setEncryptFiles(true);
        zipParameters.setCompressionLevel(CompressionLevel.HIGHER);
        zipParameters.setEncryptionMethod(EncryptionMethod.AES);
        ZipFile zipFile = new ZipFile("D:\\idea\\file\\output1.zip", "password".toCharArray());
        zipFile.addFile(new File("D:\\idea\\file\\test.csv"), zipParameters);
//        File fileToAdd = new File("D:\\idea\\file\\test.csv");
//        if (!fileToAdd.exists()) {
//            fileToAdd.createNewFile();
//        }
    }
//    public static void main(String[] args) throws Exception {
//        List<String> files = new ArrayList<>();
//        files.add("D:\\idea\\file\\test.csv");
////        files.add("path/to/file2");
//        //创建压缩包
//        String zipFilePath = "D:\\idea\\file\\output.zip";
//        ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFilePath));
//        //将文件添加到压缩包
//        for (String filePath : files) {
//            File file = new File(filePath);
//            FileInputStream fileInputStream = new FileInputStream(file);
//            ZipEntry zipEntry = new ZipEntry(file.getName());
//            zipOutputStream.putNextEntry(zipEntry);
//
//            // 读取文件内容并写入压缩包
//            byte[] buffer = new byte[4096];
//            int length;
//            while ((length = fileInputStream.read(buffer)) > 0) {
//                zipOutputStream.write(buffer, 0, length);
//            }
//
//            zipOutputStream.closeEntry();
//            fileInputStream.close();
//        }
//        //加密压缩包
//        Random rand = new SecureRandom();
//        byte[] bytes = new byte[16];
//        rand.nextBytes(bytes);
//        IvParameterSpec ivSpec = new IvParameterSpec(bytes);
//        String password = "000000000000000";
//        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//        SecretKeySpec secretKeySpec = new SecretKeySpec(bytes, "AES");
////        IvParameterSpec ivParameterSpec = new IvParameterSpec(password.getBytes());
//        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec);
//        byte[] encryptedData = cipher.doFinal(Files.readAllBytes(Paths.get(zipFilePath)));
//
//        //保存压缩包到指定路径
//        String encryptedZipFilePath = "D:\\idea\\file\\output.zip";
//        Files.write(Paths.get(encryptedZipFilePath), encryptedData);
//    }
}
